{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "title and description"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "#os.environ['create_image']='True'\n",
    "#os.environ['repository']='romeokienzler'\n",
    "os.environ['install_requirements']='True'\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "papermill": {
     "duration": 2.413514,
     "end_time": "2021-01-28T15:59:34.938111",
     "exception": false,
     "start_time": "2021-01-28T15:59:32.524597",
     "status": "completed"
    },
    "scrolled": true,
    "tags": []
   },
   "outputs": [],
   "source": [
    "import os\n",
    "if bool(os.environ.get('create_image',False)):\n",
    "    docker_file=\"\"\"\n",
    "    FROM registry.access.redhat.com/ubi8/python-38\n",
    "    RUN pip install nvflare==2.0.16\n",
    "    RUN pip install tensorflow==2.9.1\n",
    "    ADD nvflare.ipynb /\n",
    "    ENTRYPOINT [\"ipython\",\"/nvflare.ipynb\"]\n",
    "    \"\"\"\n",
    "    with open(\"Dockerfile\", \"w\") as text_file: \n",
    "        text_file.write(docker_file)\n",
    "\n",
    "    !docker build -t claimed-train-nvflare .\n",
    "    !docker tag claimed-train-nvflare `echo $repository`/claimed-train-nvflare\n",
    "    !docker push `echo $repository`/claimed-train-nvflare\n",
    "elif bool(os.environ.get('install_requirements',False)):\n",
    "    !pip install tensorflow==2.9.1 tensorflow-hub==0.12.0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import tensorflow as tf\n",
    "# from tensorflow.keras.applications import ResNet50V2 # , MobileNetV3Small\n",
    "from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout\n",
    "from tensorflow.keras import Model\n",
    "from claimed_utils import unzip, zipdir\n",
    "import os.path\n",
    "import glob\n",
    "from io import BytesIO\n",
    "from minio import Minio\n",
    "from ansible.module_utils.parsing.convert_bool import boolean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# @dependency codait_utils.ipynb\n",
    "# @param image_shape shape the images shall be scaled to and the\n",
    "# model then will accept\n",
    "# @param model zip file name\n",
    "# @param data zip file name\n",
    "# @param model folder name\n",
    "# @param data folder name\n",
    "# @param epochs number of epochs to train\n",
    "# @param checkpoint activate checkpointing\n",
    "# @param checkpoint_ip minio endpoint\n",
    "# @param checkpoint_user minio user\n",
    "# @param checkpoint_pass minio pw\n",
    "# @param checkpoint_bucket minio bucket\n",
    "# @returns model zip file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "image_shape = os.environ.get('image_shape', '400,400')\n",
    "model_zip = os.environ.get('model_zip', 'model.zip')\n",
    "data_zip = os.environ.get('data_zip', 'data.zip')\n",
    "model_folder = os.environ.get('model', 'model')\n",
    "data = os.environ.get('data', 'data')\n",
    "epochs = int(os.environ.get('epochs', 1))\n",
    "checkpoint = boolean(os.environ.get('checkpoint', False))\n",
    "checkpoint_ip = os.environ.get('checkpoint_ip')\n",
    "checkpoint_user = os.environ.get('checkpoint_user', 'minio')\n",
    "checkpoint_pass = os.environ.get('checkpoint_pass', 'minio123')\n",
    "checkpoint_bucket = os.environ.get('checkpoint_bucket', 'checkpoint')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "exists = False\n",
    "\n",
    "if checkpoint:\n",
    "    client = Minio(checkpoint_ip,\n",
    "                   checkpoint_user,\n",
    "                   checkpoint_pass,\n",
    "                   secure=False)\n",
    "\n",
    "    objects = client.list_objects(checkpoint_bucket)\n",
    "    asset_name = model_zip\n",
    "    for obj in objects:\n",
    "        if asset_name == obj.object_name:\n",
    "            exists = True\n",
    "            client.fget_object(checkpoint_bucket, model_zip, model_zip)\n",
    "            break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "# TF1 version\n",
    "import tensorflow.compat.v1 as tf\n",
    "import tensorflow_hub as hub\n",
    "tf.config.run_functions_eagerly(False)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_handle = 'https://tfhub.dev/google/tiny_video_net/tvn2/1'\n",
    "model = hub.Module(model_handle)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "vid_placeholder = tf.placeholder(tf.float32,\n",
    "                                 shape=(batch_size * num_frames,\n",
    "                                        image_size, image_size, 3))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This generates a random video. It should be replaced by a user's video.\n",
    "# video = load_video(video_path) which should return a video of the above shape.\n",
    "video = np.random.rand(*vid_placeholder.shape)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "predictions = model(video)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_link='https://tfhub.dev/google/tiny_video_net/tvn2/1'\n",
    "model_link=$model_link'?tf-hub-format=compressed'\n",
    "wget $model_link -O model\n",
    "tar xvzf model\n",
    "rm model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash\n",
    "mkdir tvn2\n",
    "mv assets tvn2/\n",
    "mv variables tvn2/\n",
    "mv *.pb tvn2/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "import tensorflow_hub as hub\n",
    "tf.compat.v1.enable_resource_variables()\n",
    "model = hub.load(\"tvn2\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "vid_placeholder = tf.placeholder(tf.float32,\n",
    "                                 shape=(batch_size * num_frames,\n",
    "                                        image_size, image_size, 3))\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "papermill": {
   "default_parameters": {},
   "duration": 55.042719,
   "end_time": "2021-01-28T16:00:26.871724",
   "environment_variables": {},
   "exception": null,
   "input_path": "/home/jovyan/work/elyra-classification/train-trusted-ai.ipynb",
   "output_path": "/home/jovyan/work/elyra-classification/train-trusted-ai.ipynb",
   "parameters": {},
   "start_time": "2021-01-28T15:59:31.829005",
   "version": "2.2.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
